package com.project.frba.bifront.client.view.descriptors;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.FitLayout;
import com.project.frba.bifront.client.model.FrontDescriptor;
import com.project.frba.bifront.client.rpc.ETLServices;
import com.project.frba.bifront.client.rpc.ETLServicesAsync;
import com.project.frba.bifront.client.view.MessageWindow;

public class DescriptorForm extends Composite {

	private Window mainWindow = new Window("Descriptor");
	
	private FrontDescriptor descriptor;
	
	private Button btSave = new Button("Guardar");
	
	private Button btCancel = new Button("Cancelar");
	
	private TextField tfDelay;

	public DescriptorForm(FrontDescriptor descriptor, DescriptorList me) {
		this.descriptor = descriptor;
		final FormPanel formPanel = new FormPanel();
		formPanel.setPaddings(4);
		
		tfDelay = new TextField("Demora", "delay", 160);
		formPanel.add(tfDelay);
		
		Label lblUrl = new Label();
		lblUrl.setHtml("" +
				"<div style=\"width: 100%; font-size: 14px;\" align=\"center\" >" +
					"<a href=\"./uploadServer/" + descriptor.getFilePath().substring(descriptor.getFilePath().lastIndexOf("/") + 1, descriptor.getFilePath().length()) +"\">Descargar el archivo descriptor</a>" +
				"</div>");
		formPanel.add(lblUrl);
		
		 
		loadActions();
		mainWindow.setTitle("Descriptor - " + descriptor.getLabel());
		mainWindow.setLayout(new FitLayout());
		mainWindow.setModal(true);
		mainWindow.setWidth(340);
		mainWindow.setHeight(170);
		mainWindow.add(formPanel);
		mainWindow.setButtons(new Button[]{btSave, btCancel});
		initWidget(mainWindow);
		mainWindow.show();
	}

	private void loadActions() {
		btSave.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				ETLServicesAsync etlServices = (ETLServicesAsync)GWT.create(ETLServices.class);
				AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>(){
					public void onFailure(Throwable caught) {
						new MessageWindow("No se puedo cambiar el período de ejecución", true);
					}
					
					public void onSuccess(Boolean result) {
						if(result.equals(true)){
							new MessageWindow("Se ha modificado el período de ejecución satisfactoriamente.", true);
							mainWindow.close();
						} else {
							new MessageWindow("Error: expresion de demora no valida. Consulte el manual de usuario.", true);
						}
						
					}
				};
				
				etlServices.changeExecutionTime(descriptor.getDescriptorName(), tfDelay.getValueAsString(), callback);
			}
		});
		
		btCancel.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				mainWindow.close();
			}
		});
		
	}

}
